package com.yizhang.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.yizhang.mq.producer.ProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author: yi.zhang
 * @CreateTime: 2025-06-27 16:22:06
 * @Description:
 */
@RestController
public class HelloController {

    @Autowired
    private ProducerService producerService;
    private RedisTemplate redisTemplate;

    @PreAuthorize("hasAuthority('write')") // 进入接口前校验
    @GetMapping("hello")
    public String hello() {
        return "hello";
    }

    @GetMapping("incr")
    public void incr() {
//        List<Map<String, Object>> list = articles();
//        String str = JSON.toJSONString(list);
//        redisTemplate.opsForValue().set("list", str);
        String list1 = redisTemplate.opsForValue().get("list").toString();
        JSONArray objects = JSON.parseArray(list1);
        List<Map> javaList = objects.toJavaList(Map.class);
        for (Map map : javaList) {
            System.out.println(map);
        }
    }

    @GetMapping("/send")
    public void sendMsg(@RequestParam("message") String message) {
        producerService.send(message);
    }
}
